package org.swan.web.role.controller;

import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import org.stream.base.MessageBean;
import org.swan.web.role.bean.RolePermissionBean;
import org.swan.web.role.service.RolePermissionService;
import java.util.List;

@RestController
@RequestMapping("/role_permission")
public class RolePermissionController {

    private final RolePermissionService rolePermissionService;

    public RolePermissionController(RolePermissionService rolePermissionService) {
        this.rolePermissionService = rolePermissionService;
    }

    @GetMapping("/get")
    public MessageBean<RolePermissionBean> get(@RequestParam("rolePermissionId") long rolePermissionId) {
        return MessageBean.MSG_SUCCESS.setData(rolePermissionService.getRolePermissionById(rolePermissionId));
    }

    @PostMapping("add")
    public MessageBean<RolePermissionBean> add(@RequestBody RolePermissionBean rolePermissionBean) {
        long rolePermissionId = rolePermissionService.addRolePermission(rolePermissionBean);
        return MessageBean.MSG_SUCCESS.setData(rolePermissionService.getRolePermissionById(rolePermissionBean.getRolePermissionId()));
    }

    @PostMapping("update")
    public MessageBean<RolePermissionBean> update(@RequestBody RolePermissionBean rolePermissionBean) {
        return MessageBean.MSG_SUCCESS.setData(rolePermissionService.updateRolePermission(rolePermissionBean));
    }

    @GetMapping("delete")
    public MessageBean<RolePermissionBean> delete(@RequestParam("rolePermissionId") long rolePermissionId) {
        return MessageBean.MSG_SUCCESS.setData(rolePermissionService.deleteRolePermissionById(rolePermissionId));
    }

    @GetMapping("list")
    public MessageBean<List<RolePermissionBean>> list(@Param("offset") int offset, @Param("rows") int rows) {
        return MessageBean.MSG_SUCCESS.setData(rolePermissionService.list(offset, rows));
    }
}
